package com.inspur.edp.common.expr.parser;

import com.inspur.edp.common.expr.ast.CompileUnitParser;
import org.antlr.v4.runtime.tree.TerminalNode;

/**
 * @author lizhaorui
 * @date 2025/8/18
 * @description
 */
public class NameAstParser {

    public static String getNameStr(CompileUnitParser.NameContext nameContext) {
        StringBuilder sb = new StringBuilder();
        NameAstParser.appendName(nameContext, sb);
        return sb.toString();
    }

    private static void appendName(CompileUnitParser.NameContext ctx, StringBuilder sb) {
        if (ctx == null) {
            return;
        }
        appendName(ctx.name(), sb);
        visitTerminal(ctx.Dot(), sb);
        visitTerminal(ctx.SimpleName(), sb);
    }

    public static void visitTerminal(TerminalNode node, StringBuilder sb) {
        if (node == null) {
            return;
        }
        sb.append(node.getText());
    }
}
